package com.cost.service.impl;

import com.cost.dao.CostInvoiceDao;
import com.cost.dao.InvoiceAddressDao;
import com.cost.dao.InvoiceInformationDao;
import com.cost.service.CostInvoiceService;
import com.cost.vo.Entity;
import com.cost.vo.InvoiceAddress;
import com.cost.vo.InvoiceInformation;
import com.rmb.mart.entity.cost.CostInvoice;
import com.rmb.mart.utils.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (CostInvoice)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 14:04:20
 */
@Service("costInvoiceService")
public class CostInvoiceServiceImpl implements CostInvoiceService {
    @Resource
    private CostInvoiceDao costInvoiceDao;

    @Resource
    private IdWorker idWorker;

    @Resource
    private InvoiceAddressDao invoiceAddressDao;

    @Resource
    private InvoiceInformationDao invoiceInformationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CostInvoice queryById(Long id) {
        return this.costInvoiceDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CostInvoice> queryAllByLimit(int offset, int limit) {
        return this.costInvoiceDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param costInvoice 实例对象
     * @return 实例对象
     */
    @Override
    public CostInvoice insert(CostInvoice costInvoice) {
        this.costInvoiceDao.insert(costInvoice);
        return costInvoice;
    }

    /**
     * 修改数据
     *
     * @param costInvoice 实例对象
     * @return 实例对象
     */
    @Override
    public CostInvoice update(CostInvoice costInvoice) {
        this.costInvoiceDao.update(costInvoice);
        return this.queryById(costInvoice.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.costInvoiceDao.deleteById(id) > 0;
    }

    /**
     * 查询所用数据
     *
     * @return 所用数据
     */

    @Override
    public List<CostInvoice> selectAll() {
        return this.costInvoiceDao.queryAll(null);
    }

    /**
     * 查询出用户的所用的发票
     * @param costInvoice 查询的字段
     * @return
     */
    @Override
    public List<CostInvoice> selectAllCondition(CostInvoice costInvoice) {
        List<CostInvoice> costInvoices = new ArrayList<>();
        if (costInvoice!=null){
            Long uid = costInvoice.getUid();
            if (uid!=null){
                costInvoices = this.costInvoiceDao.queryAll(costInvoice);
            }
            return costInvoices;
        }else {
            return costInvoices;
        }
    }

    /**
     * 需要发票
     * @param entity 发票内容
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertDemo(Entity entity) {
        entity.setInvoiceAddressId(idWorker.nextId());
        entity.setInvoiceInformationId(idWorker.nextId());

        InvoiceAddress invoiceAddress = new InvoiceAddress();
        invoiceAddress.setCompanyName(entity.getCompanyName());
        invoiceAddress.setConsigneeName(entity.getConsigneeName());
        invoiceAddress.setConsigneePhone(entity.getConsigneePhone());
        invoiceAddress.setConsigneeTelephone(entity.getConsigneeTelephone());
        invoiceAddress.setConsigneeCountry(entity.getConsigneeCountry());
        invoiceAddress.setConsigneeCity(entity.getConsigneeCity());
        invoiceAddress.setCostInvoiceId(entity.getInvoiceAddressId());
        invoiceAddressDao.insert(invoiceAddress);

        InvoiceInformation invoiceInformation = new InvoiceInformation();
        invoiceInformation.setId(entity.getInvoiceInformationId());
        invoiceInformation.setCompanyName(entity.getCompanyName());
        invoiceInformation.setBossName(entity.getBossName());
        invoiceInformation.setAccountName(entity.getAccountName());
        invoiceInformation.setAccountPassword(entity.getAccountPassword());
        invoiceInformation.setAccountPhone(entity.getAccountPhone());
        invoiceInformation.setCompanyAddress(entity.getCompanyAddress());
        invoiceInformation.setCostInvoiceId(entity.getInvoiceInformationId());
        invoiceInformationDao.insert(invoiceInformation);

        CostInvoice costInvoice = new CostInvoice();
        costInvoice.setInvoiceDate(new Date());
        costInvoice.setInvoiceStatus(1);
        costInvoice.setInvoiceAddressId(entity.getInvoiceAddressId());
        costInvoice.setInvoiceInformationId(entity.getInvoiceInformationId());
        this.costInvoiceDao.insert(costInvoice);
    }
}